//
//  UpdatePwdViewController.m
//  ZhuiKe55like
//
//  Created by junseek on 15-2-2.
//  Copyright (c) 2015年 五五来客 李江. All rights reserved.
//

#import "UpdatePwdViewController.h"
#import "NetEngine.h"
#import "NSObject+JSONCategories.h"
#import "MoreBlankTableViewHeaderFooterView.h"

@interface UpdatePwdViewController ()<UITextFieldDelegate>{
    NSMutableArray *dataArrray;
    UITextField *txtOldPwd;
    UITextField *txtNewPwd;
    UITextField *txtNewPwd2;
}

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation UpdatePwdViewController
@synthesize tableView = _tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initComponents];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    //注册监听键盘事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];    
}
- (void)initComponents{
    [self.view setBackgroundColor:rgbGray];
    
    
    dataArrray =[[NSMutableArray alloc] initWithObjects:@"当前密码",@"新密码",@"确认密码",@"", nil];
    // indexType=0;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth,kContentHeight)];
    //    _tableView.layer.borderColor = [RGBACOLOR(225, 225, 225, 1.0) CGColor];
    //    _tableView.layer.borderWidth = 1.0f;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setBackgroundColor:rgbGray];
//    [_tableView showRefresh:NO LoadMore:NO];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self.view addSubview:_tableView];
    [_tableView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];//08-09修改-热点、电话 页面调整
    
    [_tableView registerClass:[MoreBlankTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"MoreBlankTableViewHeaderFooterView"];
    
    
    [self refreshUserCData];
}
-(void)refreshUserCData{
    //    DLog(@"userInfo:%@",self.userInfo);
    //    if ([self.otherInfo isEqualToString:@"update"] || [self.otherInfo isEqualToString:@"updateHome"] || [self.otherInfo isEqualToString:@"updateGroup"]) {
    //        selectGroupArray=[NSMutableArray arrayWithArray:[[self.userInfo valueForJSONStrKey:@"cid"] componentsSeparatedByString:@","]];
    //    }
    //    indexSex=[[self.userInfo valueForJSONStrKey:@"isMan"] isEqualToString:@"男"]?0:1;
    //    arrayType=[ZKSqlData selectCustomer_sort];
    //    blackY=[[self.userInfo valueForJSONStrKey:@"black"] isEqualToString:@"1"]?1:0;
    //    importantY=[[self.userInfo valueForJSONStrKey:@"important"] isEqualToString:@"1"]?1:0;
    //    DLog(@"arrayType:%@",arrayType);
    //    [_tableView reloadData];
    //
}
#pragma mark selector
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    
    [UIView animateWithDuration:0.3 animations:^{
        _tableView.frame=CGRectMake(X(_tableView), Y(_tableView), W(_tableView),self.view.frame.size.height- Y(_tableView)-distanceToMove);
        
        
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        
        _tableView.frame=CGRectMake(0, Y(_tableView), kScreenWidth,kContentHeight);
        
        
    }];
}//

#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    if (textField==txtNewPwd2) {
        [self submintButtonClicked];
    }
    
    return YES;
}

#pragma mark tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            
            return 1;
        }
        case 1:
        {
            return 2;
        }case 2:
        {
            return 1;
        }
        default:
            break;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            return 44;
        }
        case 1:
        {
            return 44;
        }
        case 2:
        {
            return 89;
        }
        default:
            break;
    }
    
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        
        {
            //当前密码
            
            //手机号
            NSString *str= [dataArrray objectAtIndex:indexPath.section];
            
            // DLog(@"_____%@",dic);
            NSString *identifier = str.description.md5;
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                cell.backgroundColor=[UIColor clearColor];
                
                UIView *BGV=[[UIView alloc]initWithFrame:cell.bounds];
                [BGV setBackgroundColor:RGBCOLOR(255, 255, 255)];
                [cell setBackgroundView: BGV];
                
                
                
                UILabel *lblName=[RHMethods labelWithFrame:CGRectMake(10, 12, 0, 20) font:Font(16) color:rgbTxtGray text:str];
                [cell addSubview:lblName];
                
                txtOldPwd=[RHMethods textFieldlWithFrame:CGRectMake(XW(lblName), 0, kScreenWidth-10-(XW(lblName)), 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@""];
                [txtOldPwd setTextAlignment:NSTextAlignmentRight];
                [txtOldPwd setSecureTextEntry:YES];
                [cell addSubview:txtOldPwd];
                [txtOldPwd setDelegate:self];
                
                UIImageView *cellLine=[RHMethods imageviewWithFrame:CGRectMake(0, 43, kScreenWidth, 1) defaultimage:@"userLine.png"];
                [cellLine setContentMode:UIViewContentModeScaleAspectFill];
                [cell addSubview:cellLine];
                
                
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                
                
            }
            
            return cell;
        }
        case 1:
        {
            //新密码、确认密码
            NSString *str= [dataArrray objectAtIndex:(indexPath.row + indexPath.section)];
            
            // DLog(@"_____%@",dic);
            NSString *identifier = str.description.md5;
            
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                cell.backgroundColor=[UIColor clearColor];
                
                UIView *BGV=[[UIView alloc]initWithFrame:cell.bounds];
                [BGV setBackgroundColor:RGBCOLOR(255, 255, 255)];
                [cell setBackgroundView: BGV];
                
                
                
                UILabel *lblName=[RHMethods labelWithFrame:CGRectMake(10, 12, 0, 20) font:Font(16) color:rgbTxtGray text:str];
                [cell addSubview:lblName];
                
                if (indexPath.row==0) {
                    
                    txtNewPwd=[RHMethods textFieldlWithFrame:CGRectMake(XW(lblName), 0, kScreenWidth-10-(XW(lblName)), 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@""];
                    [cell addSubview:txtNewPwd];
                    [txtNewPwd setTextAlignment:NSTextAlignmentRight];
                    [txtNewPwd setSecureTextEntry:YES];
                    [txtNewPwd setDelegate:self];
                }else{
                    
                    txtNewPwd2=[RHMethods textFieldlWithFrame:CGRectMake(XW(lblName), 0, kScreenWidth-10-(XW(lblName)), 44) font:fontTitle color:rgbTitleColor placeholder:@"请输入" text:@""];
                    [cell addSubview:txtNewPwd2];
                    [txtNewPwd2 setTextAlignment:NSTextAlignmentRight];
                    [txtNewPwd2 setSecureTextEntry:YES];
                    [txtNewPwd2 setDelegate:self];
                }
                
                UIImageView *cellLine=[RHMethods imageviewWithFrame:CGRectMake(0, 43, kScreenWidth, 1) defaultimage:@"userLine.png"];
                [cellLine setContentMode:UIViewContentModeScaleAspectFill];
                [cell addSubview:cellLine];
                
                
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                
                
            }
            
            return cell;
            
            
        }case 2:
        {
            //提交
            static NSString *cellIdentifier = @"userContentSubmint";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                [cell setBackgroundColor:[UIColor clearColor]];
                
                UIButton *btnSub=[RHMethods buttonWithFrame:CGRectMake(10, 10, kScreenWidth-20, 44) title:@"确定" image:@"" bgimage:@"LoginbtnBG"];
                // [btnSub setTitleEdgeInsets:UIEdgeInsetsMake(0, -280, 0, 0)];
                btnSub.titleLabel.font=Font(20);
                [btnSub setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btnSub addTarget:self action:@selector(submintButtonClicked) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btnSub];
                
            }
            
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
            
        }
        default:
            break;
    }
    
    return nil;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MoreBlankTableViewHeaderFooterView *sectionView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MoreBlankTableViewHeaderFooterView"];
    return sectionView;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   
}

-(void)submintButtonClicked{
    DLog(@"确定");//ZKpubseditpass
    NSString *strOldPwd=txtOldPwd.text;
    if (![strOldPwd notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"亲！还没有输入现在的密码哦"];
        return;
    }
    NSString *strNewPwd=txtNewPwd.text;
    if (![strNewPwd notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"亲！是不是忘了填什么东西啊"];
        return;
    }
    NSString *strNewPwd2=txtNewPwd2.text;
    if (![strNewPwd2 notEmptyOrNull]) {
        [SVProgressHUD showImage:nil status:@"亲！是不是忘了填什么东西啊"];
        return;
    }
    if (![strNewPwd isEqualToString:strNewPwd2]) {
        [SVProgressHUD showImage:nil status:@"哎呀！好像新密码不一样诶..."];
        return;
    }
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithCapacity:0];
    [dic setValue:[[Utility Share] userId] forKey:@"uid"];
    [dic setValue:[[Utility Share] userToken] forKey:@"token"];
    [dic setValue:strOldPwd  forKey:@"oldpass"];
    [dic setValue:strNewPwd forKey:@"newpass"];
    [dic setValue:strNewPwd2 forKey:@"repass"];
    
    [NetEngine createPostAction:XKFusereditpass withParams:dic onCompletion:^(id resData, BOOL isCache) {
        DLog(@"resData:%@",resData);
        if ([[resData valueForJSONKey:@"status"] isEqualToString:@"200"]) {
            [[Utility Share] setUserPwd:strNewPwd];
            [[Utility Share] saveUserInfoToDefault];
            [self performSelector:@selector(backButtonClicked:) withObject:nil afterDelay:0.8];
            [SVProgressHUD showSuccessWithStatus:[resData valueForJSONStrKey:@"info"]];
        }else{
            [SVProgressHUD showErrorWithStatus:[resData valueForJSONKey:@"info"]];
        }
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
